Fraction
Lossless fractions and decimals; drop-in float replacement
Features
- Fraction type, representing floats as fractions
- Decimal type, based on Fraction type with explicit precision
- Fractions are drop-in replacement for floats with the exception for NaN == NaN && NaN < NegInfinity, thus it's hasheable and orderable.
- Fractions are hashable and orderable, so they may be used in Sets, HashMaps and BTrees.
- DynaInt type, which is an int dynamically growing into heap. Performs checked math and avoids stack overflows.
- PostgreSQL integration for Numeric/Decimal type (with no extra memory allocations)
- Juniper integration for both fractions and decimals
- Support for generic integer conversions, such as
i8 -> u8
,usize -> u8
and so on - Lossless division with infinite precision and no memory allocations
Documentation
Examples
Simple use:
type F = Fraction; // choose the type accordingly with your needs (see prelude module docs)
let two = from + 2; // 0 + 2 = 2
let two_third = two / 3; // 2/3 = 0.666666[...]
assert_eq!;
assert_eq!;
assert_eq!; // print as Fraction (by default)
assert_eq!; // format as decimal and print up to 4 digits after floating point
Decimal is implemented as a representation layer on top of Fraction. Thus, it is also lossless and may require explicit control over "precision" for comparison and formatting operations.
type D = Decimal; // choose the type accordingly with your needs (see prelude module docs)
let result = from / 0.3;
assert_eq!; // calculation result uses precision of the operands
assert_eq!; // explicitly passing precision to format
assert_eq!; // the other way to set precision explicitly on Decimal
Construct:
Fraction:
use FromStr;
use ;
// fraction crate also re-exports num::{One, Zero} traits for convenience.
use ;
// There are several ways to construct a fraction, depending on your use case
// `new` - construct with numerator/denominator and normalize the fraction.
// "Normalization" means it will always find the least common denominator
// and convert the input accordingly.
let f = new;
// `new_generic` - construct with numerator/denominator of different integer types
assert_eq!;
// `from` - converts from primitive types such as i32 and f32.
assert_eq!; // convert from float (f32, f64)
// `from_str` - tries parse a string fraction. Supports the usual decimal notation.
assert_eq!; // parse a string
// `from_str` - also supports _fraction_ notation such as "numerator/denominator" delimited by slash (`/`).
assert_eq!; // parse a string
// `new_raw` - construct with numerator/denominator but do not normalize the fraction.
// This is the most performant constructor, but does not calculate the common denominator,
// so may lead to unexpected results in following calculations if the fraction is not normalised.
// WARNING: Only use if you are sure numerator/denominator are already normalized.
assert_eq!;
// `one` - implements num::One trait
assert_eq!;
// `zero` - implements num::Zero trait
assert_eq!;
Decimal:
use FromStr;
use ;
Convert into/from other types
Both fraction
and decimal
types implement
from
andtry_into
for all built-in primitive types.from
andtry_into
forBigInt
andBigUint
whenwith-bigint
feature enabled.
use ;
use TryInto;
// Convert from examples (from primitives always succeed)
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
// Convert into examples (try_into returns Result<T, ()>)
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Format (convert to string)
Formatting works the same for both Decimal and Fraction (Decimal uses Fraction internally). The format implementation closely follows the rust Format trait documentation.
type F = Fraction;
let result = from / 0.4;
assert_eq!;
assert_eq!;
assert_eq!;
Generic integer constructor (construct with loose num/denom types)
If you have numerator
and denominator
of two incompatible types,
which cannot be implicitly casted to a single common type.
E.g.
- numerator
i32
- denominator
u32
use ;
type F = ;
let fra = new_generic.unwrap;
assert_eq!;
Postgres usage notes
It is recommended to use Decimal over Fraction for PostgreSQL interactions. When interacting with PostgreSQL, fraction type keeps the highest achievable precision up to 16383 digits after floating point. That may lead to suboptimal performance for such values as 1/3 or 1/7. Decimal has its own explicit precision, so there won't be accidental calculation of tens of thousands digits.
PostgreSQL uses i16 for its binary protocol, so you'll have to use at least u16 as the base type for your GenericFraction/GenericDecimal. However, it is also possible to workaround via DynaInt<u8, something_more_than_u8>.
It is recommended to use DynaInt<usize, BigUint>
so that by default you have on-stack math, and if necessary
heap memory gets allocated.
Otherwise, both types (fractions and decimals) should work transparently in accordance with Postgres crate documentation.
Change Log
Look into the CHANGELOG.md file for details